﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.Security;
using System.Security.Cryptography;

namespace MMM.Services.Expansion
{
    public class CommonFunction
    {
        public static class Commonfunction
        {
            //用MD5对文件的一个加密
            public static String GetStreamMD5(Stream stream)
            {
                string strResult = "";
                string strHashData = "";
                byte[] arrbyHashValue = null;
                System.Security.Cryptography.MD5CryptoServiceProvider md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();//实例化MD5对象
                arrbyHashValue = md5.ComputeHash(stream);//计算制定stream对象的哈希值
                strHashData = System.BitConverter.ToString(arrbyHashValue);//由字符分割的十六进制构成的string，其中对表示value中的对应元素
                strHashData = strHashData.Replace("-", "");//去掉“-”
                strResult = strHashData;
                return strResult;
            }
            //MD5加密
            public static string MD5Encrypt(string pass)
            {
                //return FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "MD5").ToLower();
                return System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(pass, "MD5").ToLower().Substring(8, 16);

            }
            /// <summary>
            /// 限制字符串的长短
            /// </summary>
            /// <param name="str">字符串</param>
            /// <param name="num">数量</param>
            /// <returns></returns>
            public static string GetPartContent(string str, int num)
            {
                int lenb = 0;
                for (int i = 0; i < str.Length; i++)
                {
                    if (str[i] > 255)
                    {
                        lenb += 2;
                    }
                    else
                    {
                        lenb += 1;
                    }
                }
                if (lenb >= num)
                {
                    int tmpint = 0;
                    string tmpstr = string.Empty;
                    for (int j = 0; j < str.Length; j++)
                    {
                        if (tmpint < num)
                        {
                            tmpstr += str[j];
                            if (str[j] > 255)
                            {
                                tmpint += 2;
                            }
                            else
                            {
                                tmpint += 1;
                            }
                        }
                    }
                    return tmpstr;
                }
                else
                {
                    return str;
                }
            }
            public static string ToMD5Str(string str)
            {
                string strSecurityedstr = FormsAuthentication.HashPasswordForStoringInConfigFile(str, "MD5");
                return strSecurityedstr;
            }
            public static string GetMD5Str(string str)
            {
                // First we need to convert the string into bytes, which
                // means using a text encoder.
                byte[] unicodeText = System.Text.Encoding.UTF8.GetBytes(str);

                // Now that we have a byte array we can ask the CSP to hash it
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] result = md5.ComputeHash(unicodeText);

                // Build the final string by converting each byte
                // into hex and appending it to a StringBuilder
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < result.Length; i++)
                {
                    sb.Append(result[i].ToString("x2"));
                }
                return sb.ToString().ToLower();
            }


            /// <summary>
            /// sunys:2012-9-20
            /// 转换可以为空的DateTime
            /// </summary>
            /// <param name="str"></param>
            /// <returns></returns>
            public static DateTime? GetTime(object str)
            {
                if (str == DBNull.Value)
                {
                    return null;
                }
                else
                {
                    return Convert.ToDateTime(str);
                }
            }

            /// <summary>
            /// 获得自增长顺序号，每天从1开始
            /// </summary>
            /// <param name="key">关键字</param>
            /// <returns></returns>
            public static int GetIntervalID(string key) {
                return new Random().Next();
            }
        }
    }
}
